package com.whu.MenuHub.controller;

import com.whu.MenuHub.domain.User;
import com.whu.MenuHub.exception.MenuHubException;
import com.whu.MenuHub.security.DbUserDetailService;
import com.whu.MenuHub.security.JwtTokenUtil;
import com.whu.MenuHub.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

@RestController
@CrossOrigin
@RequestMapping("authenticate")
@Api(tags = "登录处理API")
public class AuthenticationController {

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    PasswordEncoder passwordEncoder;

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private UserService userService;

    @Autowired
    FoodController foodController;

    @Autowired
    LikePredicController likePredicController;

    @PostMapping("/login")
    @ApiOperation("根据user判断用户信息是否正确")
    public String login(@RequestBody User user) throws MenuHubException {
        //如果没找到用户，userDetailsService会排除异常
        final UserDetails userDetails = userDetailsService.loadUserByUsername(user.getUsername());
        if(userDetails==null){
            throw new MenuHubException(MenuHubException.ErrorType.LOGIN_ERROR,"用户信息不存在");
        }
        //获取到用户的身份
        Collection<GrantedAuthority> authorities = (Collection<GrantedAuthority>) userDetails.getAuthorities();
        GrantedAuthority authority=new SimpleGrantedAuthority(user.getRole());
        //判断用户的身份是否正确
        if(!authorities.contains(authority)){
            throw new MenuHubException(MenuHubException.ErrorType.LOGIN_ERROR,"用户信息不存在");
        }
        if (passwordEncoder.matches(user.getPassword(),userDetails.getPassword())) {
            final String token = jwtTokenUtil.generateToken(userDetails);
//            //测试专用 登录后获取评价基本信息
//            foodController.getfoodKeyWord();
//            //测试专用 登陆后更新协同矩阵
//            likePredicController.refreshSimilarity();
            System.out.println("--------------------------");
            System.out.println("用户的密码输出："+userDetails.getPassword());
            System.out.println("--------------------------");
            return token;
        } else {
                //用户密码信息错误的异常
                throw new MenuHubException(MenuHubException.ErrorType.LOGIN_ERROR,"用户认证不通过");
            }
    }
    @PostMapping("/register")
    @ApiOperation("实现注册逻辑")
    public String register(@RequestBody User user) throws MenuHubException {
        //只能是用户注册
        user.setRole("customer");
        userService.insertUser(user);
        //注册后获取一个UserDetail的对象，构建token并返回token
        final UserDetails userDetails = userDetailsService.loadUserByUsername(user.getUsername());
        final String token = jwtTokenUtil.generateToken(userDetails);
        return token;
    }
}
